// page/component/new-pages/cart/cart.js
Page({
  data: {
    carts:[],               // 购物车列表
    hasList:false,          // 列表是否有数据
    totalPrice:0,           // 总价，初始为0
    selectAllStatus:true,    // 全选状态，默认全选
    totalnumb:0
  },
  onShow() {
    
    this.setData({
      hasList: true,
      carts:[{ 
          id: 1, 
          busname: '盛亚福源自营旗舰店', 
          title: '盛亚福源 牛肉黄金烤肠 500g ', 
          image: 'http://img.ehalfresh.com/ejsimage/images/seller/17/dcd34ae0-0e98-426a-9597-fad62b0b6435.jpg', 
          num: 4, 
          price: '24.50',
          Total:0,
          Stock:"366",
          selected:true
        },{ 
           id: 2,
           busname: '北大荒自营旗舰店', 
           title: '鸡肉汉堡饼 250g ', 
           image: 'http://img.ehalfresh.com/ejsimage/images/seller/17/dcd34ae0-0e98-426a-9597-fad62b0b6435.jpg', 
           num: 1, 
           price:' 553.00',
           Total:0,
           Stock: "411",
           Subtotal:"",
           selected:true
        }
      ]
    });
    this.getTotalPrice();
    this.getTotal();
    this.getNumber()
  },
  /**
   * 当前商品选中事件
   */
  selectList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    const selected = carts[index].selected;
    carts[index].selected = !selected;
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
    this.getNumber()
  },

  /**
   * 删除购物车当前商品
   */
  deleteList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    carts.splice(index,1);
    this.setData({
      carts: carts
    });
    if(!carts.length){
      this.setData({
        hasList: false
      });
    }else{
      this.getTotalPrice();
      this.getNumber()
    }
  },

  /**
   * 购物车全选事件
   */
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let carts = this.data.carts;
    for (let i = 0; i < carts.length; i++) {
      carts[i].selected = selectAllStatus;
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      carts: carts
    });
    this.getTotalPrice();
    this.getNumber()
  },

  /**
   * 绑定加数量事件
   */
  addCount(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let num = carts[index].num;
    let Total = carts[index].Total;
    let price = carts[index].price
    num = num + 1;
    Total = (num * price).toFixed(2)
    carts[index].Total = Total;
    carts[index].num = num;
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
    this.getNumber()
  },

  /**
   * 绑定减数量事件
   */
  minusCount(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let num = carts[index].num;
    let Total = carts[index].Total
    let price = carts[index].price
    if(num <= 1){
      return false;
    }
    num = num - 1;
    Total = (num * price).toFixed(2);

    carts[index].Total = Total;
    carts[index].num = num;
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
    this.getNumber()
  },

  /**
   * 计算总价
   */
  getTotalPrice() {
    let carts = this.data.carts;                  // 获取购物车列表
    let total = 0;
    for(let i = 0; i<carts.length; i++) {         // 循环列表得到每个数据
      if(carts[i].selected) {                     // 判断选中才会计算价格
        total += carts[i].num * carts[i].price;   // 所有价格加起来
      }
    }
    this.setData({                                // 最后赋值到data中渲染到页面
      carts: carts,
      totalPrice: total.toFixed(2)
    });
  },
  // 小计
  getTotal() {
    let carts = this.data.carts;
    for (var i = 0; i < carts.length ; i++){
      let num = carts[i].num;
      let Total = carts[i].Total;
      let price = carts[i].price
      Total = (num * price).toFixed(2)
      carts[i].Total = Total;
      carts[i].num = num;
    }
    this.setData({
      carts: carts
    });
  },
  // 产品数量
  getNumber(){
    let carts = this.data.carts;
    let total= 0;
    for(var i=0;i<carts.length;i++){
      if(carts[i].selected){
        total+=carts[i].num
      }
    }
    this.setData({
      carts:carts,
      totalnumb: total
    })
  }
})